package me.macd.mindoc.dao.impl;

import me.macd.mindoc.dao.BookDao;
import me.macd.mindoc.model.BookResult;
import me.macd.mindoc.model.Books;
import me.macd.mindoc.util.DefaultNameHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: mindoc
 * @description: ${description}
 * @author: 2018-10-10
 * @create: 2018-10-10 19:46
 */
@Repository
public class BookDaoJDBCTemplateImpl implements BookDao {

    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public BookDaoJDBCTemplateImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int countForHomeToPager(int memberId) {
        String sql;
        if (memberId == 0) {
            sql = "SELECT COUNT(*) FROM md_books WHERE privately_owned = 0";
            return jdbcTemplate.queryForObject(sql, Integer.class);
        } else {
            sql = "SELECT COUNT(*) FROM md_books AS book LEFT JOIN md_relationship AS rel" +
                    " ON rel.book_id = book.book_id AND rel.member_id=?" +
                    " WHERE relationship_id > 0 OR book.privately_owned = 0";
            return jdbcTemplate.queryForObject(sql, Integer.class, memberId);
        }
    }

    @Override
    public List<BookResult> findForHomeToPager(int start, int pageSize, int memberId) {
        String sql = "SELECT book.*,rel.*,member.account AS create_name,member.real_name FROM md_books AS book";
        Object[] parems;
        if (memberId == 0) {
            sql += " LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.role_id = 0";
            parems = new Object[]{start, pageSize};
        } else {
            sql = sql + " LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id =?" +
                    " LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0";
            parems = new Object[]{memberId, start, pageSize};
        }
        sql = sql + " LEFT JOIN md_members AS member ON rel.member_id = member.member_id" +
                " WHERE rel.relationship_id > 0 OR book.privately_owned = 0 ORDER BY order_index DESC ,book.book_id DESC LIMIT ?,?";

        return jdbcTemplate.query(sql, parems, new CommonRowMapper<>(BookResult.class, new DefaultNameHandler()));
    }

    @Override
    public int countToPager(int memberId) {
        String sql = "SELECT COUNT(book.book_id) AS total_count FROM md_books AS book" +
                " LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ? WHERE rel.relationship_id > 0";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, memberId);
        return count == null ? 0 : count;
    }

    @Override
    public List<BookResult> findToPager(int start, int pageSize, int memberId) {
        String sql = "SELECT book.*,rel.member_id,rel.role_id,member.account as create_name FROM md_books AS book" +
                " LEFT JOIN md_relationship AS rel ON rel.book_id = book.book_id AND rel.member_id = ?" +
                " LEFT JOIN md_relationship AS rel1 ON rel1.book_id = book.book_id AND rel1.role_id = 0" +
                " LEFT JOIN md_members AS member ON rel.member_id = member.member_id" +
                " WHERE rel.relationship_id > 0" +
                " ORDER BY book.order_index DESC,book.book_id DESC LIMIT ?,?";
        return jdbcTemplate.query(sql, new Object[]{memberId, start, pageSize}, new CommonRowMapper<>(BookResult.class, new DefaultNameHandler()));
    }

    @Override
    public int countByIdentify(String identify) {
        String sql = "SELECT COUNT(*) FROM md_books WHERE identify=?";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, identify);
        return count == null ? 0 : count;
    }

    @Override
    public List<Books> findListByIdentify(String identify, int memberId) {
        String sql = "SELECT * FROM md_books WHERE identify=? AND member_id=?";
        return jdbcTemplate.query(sql, new Object[]{identify, memberId}, new CommonRowMapper<>(Books.class, new DefaultNameHandler()));
    }

    @Override
    public int insert(Books book) {
        return new CommonDao(jdbcTemplate).insert(book);
    }
}
